<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>角色管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <link rel="shortcut icon" th:href="@{/favicon.png}" type="image/x-icon">
  <link rel="stylesheet" type="text/css" th:href="@{/res/layui/css/layui.css}">
  <link rel="stylesheet" type="text/css" th:href="@{/res/adminui/dist/css/admin.css}">
  <script type="text/javascript" th:src="@{/res/layui/layui.js}"></script>
  <script type="text/javascript" th:src="@{/res/jquery/jquery.js}"></script>
  <link rel="stylesheet" type="text/css" th:href="@{/css/pear.css}">
  <script type="text/javascript" th:src="@{/js/pear.js}"></script>
</head>
<body class="pear-container">
<form class="layui-form" id="theForm">
  <div class="mainBox" th:fragment="theForm">
    <div class="main-container" style="margin-right: 45px">
      <div class="layui-form-item">
        <label class="layui-form-label" for="name">角色名: </label>
        <div class="layui-input-block">
          <input type="text" name="name" id="name" lay-verify="title"
                 autocomplete="off" placeholder="请输入" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label" for="code">角色码: </label>
        <div class="layui-input-block">
          <input type="text" name="code" id="code" lay-verify="title"
                 autocomplete="off" placeholder="请输入" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label" for="description">备注: </label>
        <div class="layui-input-block">
          <input type="text" name="description" id="description" lay-verify="title"
                 autocomplete="off" placeholder="请输入" class="layui-input">
        </div>
      </div>
    </div>
  </div>
  <div class="bottom" th:fragment="theBtn">
    <div class="button-container">
      <button type="submit"
              class="pear-btn pear-btn-primary pear-btn-sm"
              lay-submit=""
              lay-filter="completeFilter">
        <i class="layui-icon layui-icon-ok"></i>提交
      </button>
    </div>
  </div>
</form>
<script type="text/javascript">
  layui.use(['form', 'jquery'], function () {
    let form = layui.form;
    let $ = layui.jquery;

    form.on('submit(completeFilter)', function (data) {
      // 序列化表单数据
      const formData = $('#theForm').serializeArray();
      // 转换为 JSON 对象
      const jsonData = {};
      $.each(formData, function () {
        jsonData[this.name] = this.value;
      });
      // 发送 AJAX 请求
      $.ajax({
        url: basePath + "/save",
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(jsonData),
        success: function (d) {
          layer.msg(d.msg,
            {
              icon: 1,       //信息框与加载层的私有参数
              time: 2500     //意思是2.5秒后自动关闭
            },
            function () {   //关闭弹出框后执行的函数
              console.log("后端返回" + d.msg)
              if (d.status === 200) {
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);  // 关闭layer
              }
            });
        },
        error: function (error) {
          // 请求失败后的处理
          console.error('Error:', error)
        }
      });
      return false
    });
  })
</script>
<script type="text/javascript" th:inline="javascript">
  const basePath = [[${#httpServletRequest.getContextPath()}]] + "/role"
</script>
</body>
</html>